******************************************************
******** This file creates the file County_pscs_xxxx.dta  ********
**** input file:  $dir3\county_supply_xxx.dta  (created in $dir1\create_county_supply_curves.do)
**** input file:  $dir3\PriceVectors.csv  (contains price vector from National34.m) (saved as price.dta)
**** input files: $dir3\CSPSVectors.csv (contains CS PS vector from National34.m) (saved as cspsagg.dta)
**** input files: $dir4/MuniSourceDecoder.dta  (locates muni sources in counties)
**** input files: $dir4\County_pop.dta  (population by county)

clear matrix
drop _all
set mem 2g

***** user toggles:  sph is 1;  jeh is 2; ck is 3
global user=2    
****  insheet toggles:  $toggle==1 (insheets) $toggle==0 (doesn't insheet)
global toggle=1
****  supply toggles:  $supplytoggle==2 (corrects trapezoids) $supplytoggle==1 (shifts supply out) $supplytoggle==0 (no correction) 
global supplytoggle=2

global retail_tax_adj=0.63
***********  Note:  verify that run=="run2" is first run and run=="run28" is last run 

***sph's directories
if $user==1{
global dir1="C:\AllStephen\My Dropbox\Jon n Chris\1 - Supply Curves\"
global dir2="C:\AllStephen\My Dropbox\Jon n Chris\2 - Policy Simulation\Simulation Results"
global dir3="C:\AllStephen\My Dropbox\Jon n Chris\3 - Dist of Gains\"
global dir4="C:\AllStephen\My Dropbox\Jon n Chris\4 - Land Area Calcs\"
global dir5="C:\AllStephen\My Dropbox\Jon n Chris\5 - Probits\Data\"
}

***jeh's directories
if $user==2 {
global dir1="/Users/Jon/Dropbox/LandUse/1 - Supply Curves/"
global dir2="/Users/Jon/Dropbox/LandUse/2 - Policy Simulation/Simulation Results/"
global dir3="/Users/Jon/Dropbox/LandUse/3 - Dist of Gains/"
global dir4="/Users/Jon/Dropbox/LandUse/4 - Land Area Calcs/"
global dir5="/Users/Jon/Dropbox/LandUse/5 - Probits/Data/"
}

*********************************
*** prepare the price data
*********************************
*** price.dta is created from National34.m using the following insheet
if $toggle==1 {
cd "$dir2"

*insheet using "PriceVectors.csv", clear
*insheet using "PriceVectors_ck.csv", clear
*insheet using "PriceVectors_org.csv", clear
*insheet using "PriceVectors_old.csv", clear
insheet using "PriceVectors_new.csv", clear

save price.dta, replace
}
use price.dta, clear
rename v1 price_BAU
rename v2 price_RFS 
rename v3 price_CT
rename v4 price_LCFS
rename v5 price_SUBS
*
generate str ethtype = "corn" in 1 
replace ethtype="agres" in 2
replace ethtype="orch" in 3
replace ethtype="forest" in 4
replace ethtype="msw" in 5
replace ethtype="msw_food" in 6
replace ethtype="hec" in 7
cd "$dir3"
save temp2.dta, replace


*********************************
*** prepare the CS PS data
*********************************
*** cspsagg.dta is created from National34.m using the following insheet
if $toggle==1 {
cd "$dir2"


*insheet using "CSPSVectors.csv", clear
*insheet using "CSPSVectors_ck.csv", clear
*insheet using "CSPSVectors_org.csv", clear
*insheet using "CSPSVectors_old.csv", clear
insheet using "CSPSVectors_new.csv", clear

save cspsagg.dta, replace
}
use cspsagg.dta, clear
rename v1 BAU
rename v2 RFS 
rename v3 CT
rename v4 LCFS
rename v5 SUBS
*
generate str surplus = "CS" in 1 
replace surplus="corn" in 2
replace surplus="agres" in 3
replace surplus="orch" in 4
replace surplus="forest" in 5
replace surplus="msw" in 6
replace surplus="msw_food" in 7
replace surplus="hec" in 8
replace surplus="gas" in 9
replace surplus="trans" in 10
*
foreach X in RFS CT LCFS  SUBS{
global delCS_`X'=`X'[1]
}
global CTrev=CT[10]
global SUBSrev=SUBS[10]
*
cd "$dir3"
save temp3.dta, replace



*********************************
**** Calculate surplus for each county 
*********************************
cd "$dir3"
use county_supply_new_$supplytoggle.dta, clear
***BAU surplus
merge m:1 ethtype using temp2.dta, nogen
*
foreach X in BAU RFS CT LCFS  SUBS{
foreach Y in hec corn pulpwood {
g acres_`Y'_`X'=acres_`Y'[_n-1] + (acres_`Y'-acres_`Y'[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace acres_`Y'_`X'= acres_`Y' if price_`X' >price & run =="run28"
replace acres_`Y'_`X'= max(price_`X'/price*acres_`Y',0) if price_`X' <price & run =="run2"
}
*
g quant_tons_`X'=quant_tons[_n-1] + (quant_tons-quant_tons[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace quant_tons_`X'= quant_tons if price_`X' >price & run =="run28"
replace quant_tons_`X'= max(price_`X'/price*quant_tons,0) if price_`X' <price & run =="run2"
*
g eth_`X'=ethanol[_n-1] + (ethanol-ethanol[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace eth_`X'= ethanol if price_`X' >price & run =="run28"
replace eth_`X'= max(price_`X'/price*ethanol,0) if price_`X' <price & run =="run2"
*
g cost_`X'=cost[_n-1] + 0.5*(price_`X' + price[_n-1])*(eth_`X'-ethanol[_n-1])  if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace cost_`X'=cost if price_`X' >price & run =="run28"
replace cost_`X'= 0.5*($retail_tax_adj+price_`X')*max(price_`X'/price*ethanol,0) if price_`X' <price & run =="run2"
*
g ps_`X'=price_`X'*eth_`X'-cost_`X'
}
**** need some additional work to get the weights from quant_tons  (quant_tons not used)
collapse  acres_hec_* acres_corn_* acres_pulpwood_* ps_BAU ps_RFS ps_CT ps_LCFS ps_SUBS, by (ethtype source_id)
*
g length=length(source_id)
gen str_fips = substr(source_id,2,7)
*Handle Municipal Sources 
cd "$dir1"
merge m:1 str_fips using "MuniSourceDecoder.dta"
drop if _merge == 2
replace str_fips = str_fips2 if _merge == 3
drop state county name fips55 fips stfips str_fips2 fips_l
drop _merge
gen state = real(substr(str_fips,1,2)) if length==8
gen county = real(substr(str_fips,3,3)) if length==8
replace state=real(substr(source_id,2,2)) if length==6
replace county=real(substr(source_id,4,3)) if length==6
*
collapse (sum) acres_* ps_BAU ps_RFS ps_CT ps_LCFS ps_SUBS, by (state county)
*
foreach X in  RFS CT LCFS  SUBS{
g dps_`X'=ps_`X'-ps_BAU
}
keep state county dps* acres_*
cd "$dir3"
merge 1:1 state county using "County_pop.dta"
drop if county==0
g population=real(census2000pop)
drop str_* Q* popes* county_length state_length census2000pop estimatesbase2000 _merge 
drop if population==.
preserve
collapse (sum) population
global poptot=population[1]
restore
foreach X in  RFS CT LCFS  SUBS{
replace dps_`X'=0 if dps_`X'==.
}
g dcsps_RFS=dps_RFS +$delCS_RFS*population/$poptot
g dcsps_CT=dps_CT +$delCS_CT*population/$poptot + $CTrev*population/$poptot
g dcsps_LCFS=dps_LCFS +$delCS_LCFS*population/$poptot
g dcsps_SUBS=dps_SUBS +$delCS_SUBS*population/$poptot -$SUBSrev*population/$poptot
preserve
collapse (sum) dps*
display "RFS    "  dps_RFS[1]+$delCS_RFS
display "CT     "  dps_CT[1] + $delCS_CT + $CTrev
display "LCFS   " dps_LCFS[1] +$delCS_LCFS
display "SUBS   " dps_SUBS[1] +$delCS_SUBS -$SUBSrev
restore
keep state county population dcsps_* dps_* acres_*
cd "$dir5"
*save county_csps.dta, replace
save county_csps_new_$supplytoggle.dta, replace



